package com.magold.context;

import org.andengine.audio.sound.Sound;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.SmoothCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.BaseGameActivity;

import com.magold.game.MagoldCamera;
import com.magold.skill.cast.AttacherManager;
import com.magold.sound.ISoundNotifier;
import com.magold.sound.SE;
import com.magold.text.TextFactory;
import com.magold.text.UniversalFontsBuilder;
import com.magold.texture.TextureFactory;

public abstract class BaseMagoldActivity extends BaseGameActivity implements IContext {
	protected static final int WIDTH = 800;
	protected static final int HEIGHT = 480;

	private static final float MAX_VELOCITY_X = Float.MAX_VALUE;
	private static final float MAX_VELOCITY_Y = Float.MAX_VALUE;
	private static final float MAX_ZOOM_CHANGE_FACTOR = 0.1f;

	protected SmoothCamera mCamera;

	@Override
	public EngineOptions onCreateEngineOptions() {
		mCamera = new MagoldCamera(0, 0, WIDTH, HEIGHT, MAX_VELOCITY_X, MAX_VELOCITY_Y, MAX_ZOOM_CHANGE_FACTOR);

		EngineOptions options =
				new EngineOptions(true,
						ScreenOrientation.LANDSCAPE_FIXED,
						new RatioResolutionPolicy(WIDTH, HEIGHT),
						mCamera);
		options.getTouchOptions().setNeedsMultiTouch(true);
		options.getAudioOptions().setNeedsMusic(true);
		options.getAudioOptions().setNeedsSound(true);

		return options;
	}
	
	@Override
	public synchronized void onSurfaceCreated(final GLState pGLState) {
		TextureFactory.setContext(this);
		UniversalFontsBuilder.setContext(this);
		TextFactory.setContext(this);

		super.onSurfaceCreated(pGLState);
	}

	@Override
	public MagoldEngine getEngine() {
		return (MagoldEngine) super.getEngine();
	}

	@Override
	public Engine onCreateEngine(EngineOptions pOptions) {
		return new MagoldEngine(pOptions, getAssets());
	}

	@Override
	public synchronized void onResumeGame() {
		if (mEngine != null) super.onResumeGame();
	}

	@Override
	public synchronized void onPauseGame() {
		if (mEngine != null) super.onPauseGame();
	}

	/**
	 * 
	 * @return null if not support physics, else return the physics world.
	 */
	public PhysicsWorld getPhysicsWorld() {
		return null;
	}

	@Override
	public AttacherManager getAttacherManager() {
		if (mEngine instanceof IContext) {
			return ((IContext) mEngine).getAttacherManager();
		}
		
		return null;
	}
	
	@Override
	public void setAttacherManager(AttacherManager pAttacherManager) {
		if (mEngine instanceof IContext) {
			((IContext) mEngine).setAttacherManager(pAttacherManager);
		}
	}

	@Override
	public ISoundNotifier getSoundNotifier() {
		if (mEngine instanceof IContext) {
			return ((IContext) mEngine).getSoundNotifier();
		}
		return null;
	}

	@Override
	public void setSoundNotifier(ISoundNotifier pNotifier) {
		if (mEngine instanceof IContext) {
			((IContext) mEngine).setSoundNotifier(pNotifier);
		}
	}
	
	
	@Override
	public Sound getSoundByName(String pPath) {
		return ((IFixedEngine) mEngine).getSoundByName(pPath);
	}
	
	public void putSound(String pPath) {
		Sound sound = SE.load(this, pPath);
		((MagoldEngine) mEngine).put(pPath, sound);
	}
	
}